<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { equipApi } from '@/api/controller';
  import { ToastSuccess, Toastloading, ToastWarning } from '@/utils/public';
  const formLabelWidth = '140px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
  })
  interface formQuert {
    equip_id:number
    equip_name:string
    equip_level:number | string
    can_sale:number | string
    jd_price:number
    jd_sale_price_jd:number
    jd_return_max:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    equip_name: [{ required: true, trigger: 'blur', message: '请输入装备名称' }],
    equip_level: [{ required: true, trigger: 'blur', message: '请输入装备稀有度' }],
    can_sale: [{ required: true, trigger: 'blur', message: '请选择装备出售状态' }],
    jd_price: [{ required: true, trigger: 'blur', message: '请输入价值金豆数' }],
    jd_sale_price_jd: [{ required: true, trigger: 'blur', message: '请输入金豆出售参考价格' }],
    jd_return_max: [{ required: true, trigger: 'blur', message: '请输入系统回收金豆上限' }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      await equipApi.editEquip(form).then(res=>{
        loading.close()
        ToastSuccess('修改成功')
        closeDialog()
        $emit('updateList')
      }).catch(()=>{
        loading.close()
      })
    })
  }
  const openDialog = (query:any = {})=> {
    const {
      equip_id=0, equip_name='', equip_level='',
      can_sale='', jd_price=0, jd_sale_price_jd=0, jd_return_max=0
    } = query
    if(!equip_id) ToastWarning('缺少装备ID')
    Object.assign(form,{
      equip_id,
      equip_name,
      equip_level,
      can_sale,
      jd_price,
      jd_sale_price_jd,
      jd_return_max
    })
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="8vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="50%"
    title="编辑装备">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="装备名称" prop="equip_name">
          <el-input v-model="form.equip_name" placeholder="请输入装备名称" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="装备稀有度" prop="equip_level">
          <el-input v-model="form.equip_level" placeholder="请输入装备稀有度" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="出售状态" prop="can_sale">
          <el-select v-model="form.can_sale" placeholder="请选择装备出售状态">
            <el-option label="是" :value="1" />
            <el-option label="否" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="价值金豆数" prop="jd_price">
          <el-input type="number" v-model="form.jd_price" placeholder="请输入价值金豆数" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="金豆出售参考价格" prop="jd_sale_price_jd">
          <el-input type="number" v-model="form.jd_sale_price_jd" placeholder="请输入金豆出售参考价格" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="系统回收金豆上限" prop="jd_return_max">
          <el-input type="number" v-model="form.jd_return_max" placeholder="请输入系统回收金豆上限" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>